package org.zhadoop.stream.cql.semanticanalyzer.parser.visitor;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zhadoop.stream.cql.exception.SemanticAnalyzerException;
import org.zhadoop.stream.cql.semanticanalyzer.analyzecontext.expressiondesc.ExpressionDescribe;
import org.zhadoop.stream.cql.semanticanalyzer.parsecontextreplacer.ParseContextReplacer;
import org.zhadoop.stream.cql.semanticanalyzer.parsecontextwalker.ParseContextWalker;
import org.zhadoop.stream.cql.semanticanalyzer.parser.context.BaseExpressionParseContext;
import org.zhadoop.stream.cql.semanticanalyzer.parser.context.StreamAllColumnsContext;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;

/**
 * function 解析内容
 * 
 * @author zwd
 *
 */
public class FunctionContext extends BaseExpressionParseContext {

	private static final Logger LOG = LoggerFactory.getLogger(FunctionContext.class);

	private String name;

	private boolean isDistinct = false;

	private List<BaseExpressionParseContext> arguments;

	private StreamAllColumnsContext allColumns;

	public FunctionContext() {
		arguments = Lists.newArrayList();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isDistinct() {
		return isDistinct;
	}

	public void setDistinct(boolean isDistinct) {
		this.isDistinct = isDistinct;
	}

	public List<BaseExpressionParseContext> getArguments() {
		return arguments;
	}

	public void setArguments(List<BaseExpressionParseContext> arguments) {
		this.arguments = arguments;
	}

	public StreamAllColumnsContext getAllColumns() {
		return allColumns;
	}

	public void setAllColumns(StreamAllColumnsContext allColumns) {
		this.allColumns = allColumns;
	}

	@Override
	public void walkChildAndReplace(ParseContextReplacer replacer) {
		// TODO Auto-generated method stub

	}

	@Override
	protected ExpressionDescribe createExpressionDesc() throws SemanticAnalyzerException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(name);
		sb.append("(");
		sb.append(functionBodytoString());
		sb.append(")");
		return sb.toString();
	}

	private Object functionBodytoString() {
		StringBuilder sb = new StringBuilder();
		if(isDistinct){
			sb.append(" DISTINCT ");
		}
		
		if(allColumns != null){
			sb.append(allColumns.toString());
		}
		
		sb.append(Joiner.on(", ").join(arguments.toArray()));
		return sb.toString();
	}

	@Override
	protected void walkChild(ParseContextWalker walker) {
		// TODO Auto-generated method stub

	}

}
